import fly from './request'
import { Toast } from 'vant'
import store from '@/store'

const ajaxImg = (url = '', file = '', data = {}, method = 'post', headers = {
  'Content-Type': 'multipart/form-data'
}) => {
  let formData
  formData = new FormData()
  Object.entries(data).forEach(([key, value]) => {
    formData.append(key, value)
  })
  // console.log(file)
  formData.append('imgFile', file)

  return fly.request(url, formData, {
    method,
    headers
  }).then((res) => {
    if (res.code === 200) {
      // 请求正常
      return Promise.resolve(res.data)
    } else {
      if (res.code === 40029) {
        Toast('请重新登录')
        store.dispatch('logout')
      } else {
        Toast(res.message)
      }
      return Promise.reject(res)
    }
  })
}

export default ajaxImg
